<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/14/014
 * Time: 17:08
 */

namespace app\api\controller;


use app\admin\model\Product;
use app\admin\model\Seller;
use app\common\controller\Api;

class Sellerproduct extends Api
{
    protected $noNeedLogin = ['*'];

    /**
     * 获取某个商家的所有商品
     */
    public function index()
    {
        $seller_id = $this->request->request('seller_id');
        $seller = Seller::get($seller_id);
        if (!$seller) {
            $this->error('没有这个商家');
        }

        $model = new \app\api\model\Sellerproduct();
        $productList = $model->where('seller_id', $seller_id)->select();
        $this->success('', $productList);
    }

    /**
     *找出所有的后台已有商品供选择
     */
    public function selectAllProduct()
    {
        $productList = Product::all();
        $this->success('', $productList);
    }

    /**
     * 找出后台已有单个商品
     * @throws \think\exception\DbException
     */
    public function selectOneProduct()
    {
        $id = $this->request->request('id');

        $result =  Product::get($id);
        if(!$result){
            $this->error('没有这个商品');
        }
        $this->success('', $result);
    }

    /**
     *
     */
    public function add()
    {
        $seller_id = $this->request->request('seller_id');
        $productcategory_id = $this->request->request('productcategory_id');
        //可以选择总后台已有的产品导入
        $product_id = $this->request->request('product_id');

        if (!Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        $category = \app\api\model\Sellerproductcategory::get($productcategory_id);
        if (!$category) {
            $this->error('没有这个分类');
        }
        if(!$product_id || !Product::get($product_id))
        {
            $model = new \app\api\model\Sellerproduct();
            $result = $model->allowField(true)->validate(true)->save($_POST);
            if ($result) {
                $this->success('成功');
            }
            $this->error($model->getError());
        }else{
            $product = Product::get($product_id);
            $model = new \app\api\model\Sellerproduct();
            $result = $model->allowField(true)->validate(true)->save([
                'seller_id' => $seller_id,
                'productcategory_id' => $productcategory_id,
                'name' => $product['name'],
                'description' => $product['description'],
                'image' => $product['image'],
                'product_address' => $product['product_address'],
                'manufacturer' => $product['manufacturer'],
                'weight' => $product['weight'],
                'price' => $product['price'],
            ]);
            if ($result) {
                $this->success('成功');
            }
            $this->error($model->getError());
        }

    }

    /**
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $id = $this->request->request('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $seller_id = $this->request->request('seller_id');
        if (!\app\admin\model\Seller::get($seller_id)) {
            $this->error('没有这个商家');
        }
        $productcategory_id = $this->request->request('productcategory_id');
        if (!\app\api\model\Sellerproductcategory::get($productcategory_id)) {
            $this->error('没有这个分类');
        }

        if (!\app\api\model\Sellerproduct::get($id)) {
            $this->error('没有这个产品');
        }
        $model = new \app\api\model\Sellerproduct();
        $result = $model->allowField(true)->validate(true)->save($_POST, ['id' => $id]);
        if ($result) {
            $this->success('成功');
        }
        $this->error($model->getError());
    }

    public function delete()
    {
        $id = $this->request->request('id');
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }

        $model = \app\api\model\Sellerproduct::get($id);
        $result = $model->delete();
        if ($result) {
            $this->success('成功');
        }
        $this->error($model->getError());
    }
}